//
//  ProductViewController.m
//  GameBoss
//
//  Created by 张艺 on 15/4/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ProductViewController.h"
#import "ProductionCell.h"
#import "ProductionViewController.h"
#import "ProductionInformation.h"

#define ScreenSize [UIScreen mainScreen].bounds.size

@interface ProductViewController ()
// tableview显示简介信息
@property (nonatomic , retain)UITableView *productionTabelView;
// 装 productionInformation的model信息
@property (nonatomic, retain) NSMutableArray *modelArr;
//// 装 body 没有对应的类型的数组信息
//@property (nonatomic, retain) NSMutableArray *bodyArr;
// pagenum
@property(nonatomic, assign) NSInteger pageNum;

@end

@implementation ProductViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
//        self.bodyArr = [NSMutableArray array];
        self.pageNum = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    


    
    self.productionTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, (667) * Screen.height / 667 - 49 - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.productionTabelView];
    self.productionTabelView.delegate = self;
    self.productionTabelView.dataSource = self;
    self.productionTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_productionTabelView release];
    
    [self loadData];
    [self addHeader];
    [self addFooter];
    
#pragma mark 夜间判断
    if ([NightMode shareNightMode].isNight) {
        [[NightMode shareNightMode] loadNightModeInfoFormDisk];
    }
}

- (void)addFooter
{
    [self.productionTabelView addFooterWithCallback:^{
        self.pageNum ++;
        [self loadData];
    }];
}

- (void)addHeader
{
    [self.productionTabelView addHeaderWithCallback:^{
        [self.modelArr removeAllObjects];
        [self loadData];
    }];
    [self.productionTabelView headerBeginRefreshing];
}


- (void)loadData
{
//    NSString *strURL = [NSString stringWithFormat:@"%@", self.idStr];
//    NSLog(@"%@",strURL);
    
    NSString *strURL = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/types/%@?pageNo=%ld&orderId=1&fmt=json&brandId=%@", self.typeId, self.pageNum, self.numID];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *data = dic[@"data"];
        for (NSMutableDictionary *bodyDic in data) {
            ProductionInformation *model = [[ProductionInformation alloc] init];
            [model setValuesForKeysWithDictionary:bodyDic];
            [self.modelArr addObject:model];
            [model release];

        }
        if (self.modelArr.count == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen.width, 200 * Screen.height / 667)];
            label.text = @"暂无匹配条件";
            label.textAlignment = NSTextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:18];
            label.textColor = [UIColor grayColor];
            [self.productionTabelView addSubview:label];
            [label release];
        }
        
        [self.productionTabelView reloadData];
        [self.productionTabelView headerEndRefreshing];
        [self.productionTabelView footerEndRefreshing];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.productionTabelView headerEndRefreshing];
        [self.productionTabelView footerEndRefreshing];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen.width, 200 * Screen.height / 667)];
        label.text = @"网络不给力啊";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        label.textColor = [UIColor grayColor];
        [self.productionTabelView addSubview:label];
        [label release];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"cell";
    ProductionCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[ProductionCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    if (self.modelArr.count != 0) {
        ProductionInformation *model = self.modelArr[indexPath.row];
        cell.title.text = model.shortName;
        if (model.price <= 0) {
            cell.price.text = @"无报价";
        }else{
            NSString *price = [NSString stringWithFormat:@"%ld 元",model.price];
            cell.price.text = price;
        }
        
        
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.idxPic]];
        
        if (model.items.count != 0) {
            
            //        NSMutableArray *arr = self.bodyArr[indexPath.row];
            if (model.items.count >= 2) {
                cell.mainType.text = model.items[0];
                cell.typeValue.text = model.items[1];
            }
            if (model.items.count >= 4) {
                
                cell.mainType.text = model.items[0];
                cell.typeValue.text = model.items[1];
                cell.otherType.text = model.items[2];
                cell.otherTypeValue.text = model.items[3];
            }
        }
    }
    
    
    return cell;
}

#pragma mark 选取白黑两种模式
- (void)setNightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.productionTabelView];
}

- (void)setLightDayModel {
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithView:self.productionTabelView];
    
}

- (void)dealloc
{
    [_idStr release];
    [_typeId release];
    [_numID release];
    [_productionTabelView release];
    [_modelArr release];
    [super dealloc];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductionViewController *production = [[ProductionViewController alloc] init];
    
//    production.DataArr = self.bodyArr[indexPath.row];
    production.model = self.modelArr[indexPath.row];
    [self.navigationController pushViewController:production animated:YES];
    [production release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UIScreen mainScreen].bounds.size.width / 4;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
